<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleImport()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="primary" ghost @click="handleExport()">
          <Icon icon="ant-design:vertical-align-bottom-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.moldCode }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasMoldBasMoldInvRefList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { basMoldInvRefDelete, basMoldInvRefListData } from '/@/api/bas/mold/basMoldInvRef';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import { downloadByUrl } from '/@/utils/file/download';

  const { t } = useI18n('bas.mold.basMoldInvRef');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('工装产品关联表管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工装编码'),
        field: 'moldCode',
        component: 'Input',
      },
      {
        label: t('工装名称'),
        field: 'basMold.name',
        component: 'Input',
      },
      {
        label: t('部门名称'),
        field: 'basMold.depName',
        component: 'Input',
      },
      {
        label: t('存货代码'),
        field: 'basInv.cinvaddcode',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'basInv.cinvcode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
      {
        label: t('工序编码'),
        field: 'operCode',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('工装编码'),
      dataIndex: 'moldCode',
      key: 'a.mold_code',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('工装尾号'),
      dataIndex: 'lastNum',
      key: 'a.lastNum',
      sorter: true,
      width: 130,
      align: 'left',
      //fixed: 'left',
    },
    {
      title: t('工装名称'),
      dataIndex: 'basMold.name',
      key: 'mold.code',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('存货代码'),
      dataIndex: 'basInv.cinvaddcode',
      key: 'inv.cinvaddcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('工序编码'),
      dataIndex: 'operCode',
      key: 'a.oper_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('所在部门'),
      dataIndex: 'basMold.depName',
      key: 'basMold.dep_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑工装产品关联表'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'bas:mold:basMoldInvRef:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除工装产品关联表'),
        popConfirm: {
          title: t('是否确认删除工装产品关联表'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'bas:mold:basMoldInvRef:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: basMoldInvRefListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/bas/mold/basMoldInvRef/exportData',
      params: getForm().getFieldsValue(),
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const res = await basMoldInvRefDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
